package cn.tedu.oa.attendance.service.impl;

import cn.tedu.oa.attendance.dao.AttendanceDao;
import cn.tedu.oa.attendance.entity.Attendance;
import cn.tedu.oa.attendance.service.AttendanceService;
import cn.tedu.oa.common.exception.ServiceException;
import cn.tedu.oa.common.service.impl.BaseServiceImpl;
import cn.tedu.oa.common.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl extends BaseServiceImpl<Attendance> implements AttendanceService {
    @Autowired
    private AttendanceDao attendanceDao;

    /*通过用户名查询考勤信息*/
    @Override
    public Attendance getAttendanceByName(String name) {
        if (StringUtils.isEmpty(name)) {
            throw new ServiceException("用户名为空");
        }
        return attendanceDao.getAttendanceByName(name);
    }

    /*分页显示考勤信息*/
    @Override
    public List<Attendance> getAttendancePagerList(String criteria, Integer pageNo, Integer pageSize) {
        if (pageNo == null || pageNo < 1 || pageSize == null || pageSize < 1) {
            throw new IllegalArgumentException("参数不合法，请联系运维人员");
        }
        int startIndex = (pageNo - 1) * pageSize + 1;
        int endIndex = startIndex + pageSize - 1;
        List<Attendance> list = attendanceDao.getAttendancePagerList(criteria, startIndex, endIndex);
        if (list == null) {
            throw new ServiceException("没有查询到数据");
        }
        return list;
    }

    /*新增考勤信息*/
    @Override
    public void add(Attendance attendance) {
        if (attendance == null) {
            throw new ServiceException("添加失败：用户信息为空");
        }
        if (attendance.getName() == null || StringUtils.isEmpty(attendance.getName().trim())) {
            throw new ServiceException("添加失败：用户名为空");
        }
        if (attendance.getType() == null || StringUtils.isEmpty(attendance.getType().trim())) {
            throw new ServiceException("添加失败：类型为空");
        }
       /* if (attendance.getIp() == null || StringUtils.isEmpty(attendance.getIp().trim())) {
            throw new ServiceException("添加失败：IP地址为空");
        }*/
        if (attendance.getStatus() == null || StringUtils.isEmpty(attendance.getStatus().trim())) {
            throw new ServiceException("添加失败：状态为空");
        }
        /*添加人*/
        attendance.setAddUser(ShiroUtils.getPrincipal().getName());
        /*添加时间*/
        attendance.setAddTime(new Date());
        /*删除状态：0正常，1已删除*/
        attendance.setIsdel(0);
        attendanceDao.add(attendance);
    }
    /*新增考勤信息*/
    @Override
    public void update(Attendance attendance) {
        if (attendance == null) {
            throw new ServiceException("修改失败：用户信息为空");
        }
        if (StringUtils.isEmpty(attendance.getName().trim())) {
            throw new ServiceException("修改失败：用户名为空");
        }
        if (StringUtils.isEmpty(attendance.getType().trim())) {
            throw new ServiceException("修改失败：类型为空");
        }
        if (StringUtils.isEmpty(attendance.getIp().trim())) {
            throw new ServiceException("修改失败：IP地址为空");
        }
        if (StringUtils.isEmpty(attendance.getStatus().trim())) {
            throw new ServiceException("修改失败：状态为空");
        }
        /*修改人*/
        attendance.setUpdateUser(ShiroUtils.getPrincipal().getName());
        /*修改时间*/
        attendance.setUpdateTime(new Date());
        /*删除状态：0正常，1已删除*/
        attendance.setIsdel(0);
        attendanceDao.update(attendance);
    }

    /*通过id查询*/
    @Override
    public Attendance getAttendanceById(Integer id) {
        if (id == null || id < 0) {
            return null;
        }
        return attendanceDao.get(id);
    }

}
